home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / amiga / asrc29k.lha / nr4hdr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-08  |  3.3 KB  |  143 lines

  1. /* Net/rom transport layer header conversion routines.
  2.  * Copyright 1989 by Daniel M. Frank, W9NK.  Permission granted for
  3.  * non-commercial distribution only.
  4.  */
  5. #include "global.h"
  6. #include "mbuf.h"
  7. #include "nr4.h"
  8.  
  9. /* Convert a net/rom transport header to host format structure.
  10.  * Return -1 if error, 0 if OK.
  11.  */
  12. int
  13. ntohnr4(hdr,bpp)
  14. register struct nr4hdr *hdr;
  15. struct mbuf **bpp;
  16. {
  17.     unsigned char tbuf[NR4MINHDR];
  18.  
  19.     if(pullup(bpp, (char *)tbuf, NR4MINHDR) < NR4MINHDR)
  20.         return -1;
  21.  
  22.     hdr->opcode = tbuf[4];
  23.  
  24.     switch(tbuf[4] & NR4OPCODE){
  25.         case NR4OPPID:        /* protocol ID extension */
  26.             hdr->u.pid.family = tbuf[0];
  27.             hdr->u.pid.proto = tbuf[1];
  28.             break;
  29.         case NR4OPCONRQ:    /* connect request */
  30.             hdr->u.conreq.myindex = tbuf[0];
  31.             hdr->u.conreq.myid = tbuf[1];
  32.             if(pullup(bpp,(char *)&(hdr->u.conreq.window), 1) < 1)
  33.                 return -1;
  34.             if(pullup(bpp,hdr->u.conreq.user,AXALEN) < AXALEN)
  35.                 return -1;
  36.             if(pullup(bpp,hdr->u.conreq.node,AXALEN) < AXALEN)
  37.                 return -1;
  38.             break;
  39.         case NR4OPCONAK:    /* connect acknowledge */
  40.             hdr->yourindex = tbuf[0];
  41.             hdr->yourid = tbuf[1];
  42.             hdr->u.conack.myindex = tbuf[2];
  43.             hdr->u.conack.myid = tbuf[3];
  44.             if(pullup(bpp,(char *)&(hdr->u.conack.window), 1) < 1)
  45.                 return -1;
  46.             break;
  47.         case NR4OPDISRQ:    /* disconnect request */
  48.             hdr->yourindex = tbuf[0];
  49.             hdr->yourid = tbuf[1];
  50.             break;
  51.         case NR4OPDISAK:    /* disconnect acknowledge */
  52.             hdr->yourindex = tbuf[0];
  53.             hdr->yourid = tbuf[1];
  54.             break;
  55.         case NR4OPINFO:        /* information frame */
  56.             hdr->yourindex = tbuf[0];
  57.             hdr->yourid = tbuf[1];
  58.             hdr->u.info.txseq = tbuf[2];
  59.             hdr->u.info.rxseq = tbuf[3];
  60.             break;
  61.         case NR4OPACK:        /* information acknowledge */
  62.             hdr->yourindex = tbuf[0];
  63.             hdr->yourid = tbuf[1];
  64.             hdr->u.ack.rxseq = tbuf[3];
  65.             break;
  66.         default:        /* what kind of frame is this? */
  67.             return -1;
  68.     }
  69.     return 0;
  70. }
  71.  
  72. /* Convert host-format level 4 header to network format */
  73. struct mbuf *
  74. htonnr4(hdr)
  75. register struct nr4hdr *hdr;
  76. {
  77.     static int16 hlen[NR4NUMOPS] = {5,20,6,5,5,5,5};
  78.     struct mbuf *rbuf;
  79.     register char *cp;
  80.     unsigned char opcode;
  81.  
  82.     opcode = hdr->opcode & NR4OPCODE;
  83.  
  84.     if(opcode >= NR4NUMOPS)
  85.         return NULLBUF;
  86.  
  87.     if(hdr == (struct nr4hdr *)NULL)
  88.         return NULLBUF;
  89.  
  90.     if((rbuf = alloc_mbuf(hlen[opcode])) == NULLBUF)
  91.         return NULLBUF;
  92.  
  93.     rbuf->cnt = hlen[opcode];
  94.     cp = rbuf->data;
  95.  
  96.     cp[4] = hdr->opcode;
  97.     
  98.     switch(opcode){
  99.         case NR4OPPID:
  100.             *cp++ = hdr->u.pid.family;
  101.             *cp = hdr->u.pid.proto;
  102.             break;
  103.         case NR4OPCONRQ:
  104.             *cp++ = hdr->u.conreq.myindex;
  105.             *cp++ = hdr->u.conreq.myid;
  106.             cp += 3; /* skip to sixth byte */
  107.             *cp++ = hdr->u.conreq.window;
  108.             memcpy(cp,hdr->u.conreq.user,AXALEN);
  109.             cp += AXALEN;
  110.             memcpy(cp,hdr->u.conreq.node,AXALEN);
  111.             cp += AXALEN;
  112.             break;
  113.         case NR4OPCONAK:
  114.             *cp++ = hdr->yourindex;
  115.             *cp++ = hdr->yourid;
  116.             *cp++ = hdr->u.conack.myindex;
  117.             *cp++ = hdr->u.conack.myid;
  118.             cp++;    /* already loaded pid */
  119.             *cp = hdr->u.conack.window;
  120.             break;
  121.         case NR4OPDISRQ:
  122.             *cp++ = hdr->yourindex;
  123.             *cp = hdr->yourid;
  124.             break;
  125.         case NR4OPDISAK:
  126.             *cp++ = hdr->yourindex;
  127.             *cp = hdr->yourid;
  128.             break;
  129.         case NR4OPINFO:
  130.             *cp++ = hdr->yourindex;
  131.             *cp++ = hdr->yourid;
  132.             *cp++ = hdr->u.info.txseq;
  133.             *cp = hdr->u.info.rxseq;
  134.             break;
  135.         case NR4OPACK:
  136.             *cp++ = hdr->yourindex;
  137.             *cp++ = hdr->yourid;
  138.             *++cp = hdr->u.ack.rxseq;    /* skip third byte (tricky yuck) */
  139.             break;
  140.     }
  141.     return rbuf;
  142. }
  143.